﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace demo
{
    public partial class YbPrePay : Form
    {
        public string bsje = "";
        public string fkm = "";
        public bool IsJs = false;
        public YbPrePay()
        {
            InitializeComponent();
        }
        public YbPrePay(ServiceRef.QueryPrePaymentCostOut payres, ServiceRef.QueryPatientInfoOut patinfo, string medicalBanlance)
        {
            InitializeComponent();

            this.labelName.Text = patinfo.PatientName;
            this.ybgzje.Text = payres.PactSelfAmount;//个帐
            this.grxjzf.Text = payres.SelfPayAmount;//个人现金支付
            this.ybtcje.Text = payres.PactFundAmount;//统筹
            this.hj.Text = payres.PayAmount;//总金额
            this.dqkye.Text = patinfo.Balance;
            this.dqybye.Text = medicalBanlance;
            this.jfhybye.Text = (Convert.ToDecimal(medicalBanlance) - Convert.ToDecimal(payres.PactSelfAmount)).ToString();
            var del = Convert.ToDecimal(payres.SelfPayAmount) - Convert.ToDecimal(patinfo.Balance);
            if (del > 0)
            {
                bsje = del.ToString();
            }
            else
            {
                bsje = "0";
            }
            this.bjje.Text = bsje;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Convert.ToDecimal(bsje) > 0)
            {
                IsJs = false;
                if (string.IsNullOrEmpty(this.textBoxFKM.Text))
                {
                    MessageBox.Show("当前结算卡余额不足，请出示付款码进行补充值 " + bsje + " 元后再结算！");
                    this.textBoxFKM.Focus();
                }
                fkm = this.textBoxFKM.Text;
            }
            else
            {
                IsJs = true;
            }
            this.Close();
        }

        private void textBoxFKM_Click(object sender, EventArgs e)
        {
            if (Convert.ToDecimal(bsje) == 0)
            {
                MessageBox.Show("当前结算卡余额充足，请点击【结算】按钮即可！");
            }
        }

        private void textBoxFKM_KeyDown(object sender, KeyEventArgs e)
        {
            if (Convert.ToDecimal(bsje) > 0)
            {                
                IsJs = false;
                if (string.IsNullOrEmpty(this.textBoxFKM.Text) || !IsValidPaymentCode(this.textBoxFKM.Text))
                {
                    MessageBox.Show("当前结算卡余额不足或付款码输入错误，请出示付款码进行补充值 " + bsje + " 元后再结算！");
                    this.textBoxFKM.Focus();
                }
                //textBoxFKM.Enabled = false;
                fkm = this.textBoxFKM.Text;
            }
            else
            {
                IsJs = true;
            }
            this.Close();
        }

        /// <summary>
        /// 校验字符串是否为微信/支付宝付款码（基于长度和前缀）
        /// </summary>
        /// <param name="code">待校验的字符串</param>
        /// <returns>true：符合付款码特征；false：不符合</returns>
        public static bool IsValidPaymentCode(string code)
        {
            // 基础校验：非空、18位长度、纯数字
            if (string.IsNullOrWhiteSpace(code) || code.Length != 18 || !IsAllDigits(code))
                return false;

            // 前缀校验：以13或28开头
            return code.StartsWith("13") || code.StartsWith("28");
        }

        /// <summary>
        /// 辅助方法：判断字符串是否全为数字
        /// </summary>
        private static bool IsAllDigits(string str)
        {
            foreach (char c in str)
            {
                if (!char.IsDigit(c))
                    return false;
            }
            return true;
        }
    }
}
